import { ArticlesResponse, Channel, ChannelResponse } from '@/types/data'
import { RootThunkAction } from '@/types/store'

import differenceBy from 'lodash/differenceBy'
import http from '@/utils/http'

const CHANNEL_KEY = 'geek-h5-channels'
export const getChannels = (): RootThunkAction => {
  return async (dispatch, getState) => {
    const {
      login: { token },
    } = getState()
    if (!!token) {
      const res = await http.get<ChannelResponse>('/user/channels')
      // console.log(res)
      dispatch({ type: 'home/getChannels', payload: res.data.data.channels })
    } else {
      const localChannels = JSON.parse(
        localStorage.getItem(CHANNEL_KEY) ?? '[]'
      ) as Channel[]

      if (localChannels.length === 0) {
        const res = await http.get<ChannelResponse>('/user/channels')
        // console.log(res)
        const channels = res.data.data.channels
        localStorage.setItem(
          CHANNEL_KEY,
          JSON.stringify(res.data.data.channels)
        )
        dispatch({ type: 'home/getChannels', payload: channels })
      } else {
        // console.log(localChannels)
        dispatch({ type: 'home/getChannels', payload: localChannels })
      }
    }
  }
}

export const getRestChannels = (): RootThunkAction => {
  return async (dispatch, getState) => {
    const {
      home: { channels },
    } = getState()
    const res = await http.get<ChannelResponse>('/channels')

    const restChannels = differenceBy(res.data.data.channels, channels, 'id')

    dispatch({ type: 'home/getRestChannels', payload: restChannels })
  }
}

export const toggleChannels = (id: number): RootThunkAction => {
  return (dispatch) => {
    dispatch({ type: 'home/toggleChannels', payload: id })
  }
}

export const delMyChannel = (channel: Channel): RootThunkAction => {
  return async (dispatch, getState) => {
    const {
      login: { token },
    } = getState()
    if (!!token) {
      await http.delete(`/user/channels/${channel.id}`)
    } else {
      const localChannels = JSON.parse(
        localStorage.getItem(CHANNEL_KEY) ?? '[]'
      ) as Channel[]
      const newChannels = localChannels.filter((item) => item.id !== channel.id)
      localStorage.setItem(CHANNEL_KEY, JSON.stringify(newChannels))
    }

    dispatch({ type: 'home/delChannel', payload: channel })
  }
}

export const addChannel = (channel: Channel): RootThunkAction => {
  return async (dispatch, getState) => {
    const {
      login: { token },
    } = getState()
    if (!!token) {
      await http.patch('/user/channels', {
        channels: [channel],
      })
    } else {
      const localChannels = JSON.parse(
        localStorage.getItem(CHANNEL_KEY) ?? '[]'
      ) as Channel[]

      const newChannels = [...localChannels, channel]

      localStorage.setItem(CHANNEL_KEY, JSON.stringify(newChannels))
    }
    dispatch({ type: 'home/addChannel', payload: channel })
  }
}

export const getArticleListByChannelId = (
  channelId: number,
  timestamp: string | null
): RootThunkAction => {
  return async (dispatch) => {
    const res = await http.get<ArticlesResponse>('/articles', {
      params: {
        channel_id: channelId,
        timestamp,
      },
    })

    dispatch({
      type: 'home/getArticleListByChannelId',
      payload: {
        channelId,
        articles: res.data.data,
      },
    })
  }
}

export const getNewestArticleList = (
  channelId: number,
  timestamp: string | null
): RootThunkAction => {
  return async (dispatch) => {
    const res = await http.get<ArticlesResponse>('/articles', {
      params: {
        channel_id: channelId,
        timestamp,
      },
    })

    dispatch({
      type: 'home/getNewestArticleList',
      payload: {
        channelId,
        articles: res.data.data,
      },
    })
  }
}
